import platform
from hashlib import md5

def get_mac_address():
    plat = platform.system().lower()
    if 'windows' == plat:
        mac_address = './address'
    else:
        mac_address = '/opt/class_net_eth0/address'
    try:
        with open(mac_address) as f:
            mac = f.read()
        mac = mac.replace(':', '-syn-')
    except FileNotFoundError:
        msg = f'{mac_address}不存在'
        print('synlog=', msg)
    except PermissionError:
        msg = f'{mac_address}存在但没有权限访问'
        print('synlog=', msg)
    except Exception as e:
        msg = f'{mac_address}异常{str(e)}'
        print('synlog=', msg)

    return mac.upper()


def get_pre_license():
    # 获取授权码
    mac_mixStr = get_mac_address()
    mac_mixStrMd5 = md5(mac_mixStr.encode('utf-8')).hexdigest().upper()
    pre_license = list(mac_mixStrMd5)
    pre_license.insert(4, 's')
    pre_license.insert(8, 'y')
    pre_license.insert(12, 'n')
    pre_license.insert(16, 'j')
    pre_license.insert(20, 'o')
    pre_license.insert(24, 'n')
    pre_license.insert(28, 'e')
    pre_license.insert(32, 's')
    pre_license = ''.join(pre_license)
    return pre_license

if __name__ == '__main__':
    print('设备授权码=', get_pre_license())


    
